package stat

import (
	"sort"

	"gddgame.cc/galaxy/common/dsl/game"
	"gddgame.cc/galaxy/utils"
)

const SettingStatName = "setting_stat"

type Threshold struct {
	Value   int           `json:"value" msg:"value"`
	Event   byte          `json:"event" msg:"event"`
	Params  []interface{} `json:"params" msg:"params"`
	Inherit string        `json:"inherit" msg:"inherit"`
}

type ThresholdSlice []Threshold

func (p ThresholdSlice) Len() int           { return len(p) }
func (p ThresholdSlice) Less(i, j int) bool { return p[i].Value < p[j].Value }
func (p ThresholdSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

// Sort is a convenience method.
func (p ThresholdSlice) Sort() { sort.Sort(p) }

type Trigger struct {
	Event     byte             `json:"event" msg:"event"`
	From      []int            `json:"from" msg:"from"`
	Target    []int            `json:"target" msg:"target"`
	Effect    utils.EffectMode `json:"effect" msg:"effect"`
	Value     int32            `json:"value" msg:"value"` // 倍数，固定值，属性值
	Direction bool             `json:"direction" msg:"direction"`
	Inherit   string           `json:"inherit" msg:"inherit"`

	Stat     *SettingStat `json:"-" msg:"-"`
	Priority int          `json:"-" msg:"-"`
}

type TriggerSlice []Trigger

type SettingStat struct {
	Key    string           `json:"key" orm:"primary" sql:"VARCHAR"`
	Name   string           `json:"name" sql:"VARCHAR"`
	Cycles []utils.CycleKey `json:"cycles" sql:"JSON"`
	Type   utils.StatType   `json:"type" sql:"TINYINT"`

	TriggerConfig   TriggerSlice   `json:"trigger_config" sql:"JSON"`
	ThresholdConfig ThresholdSlice `json:"threshold_config" sql:"JSON"`

	AutoNotice bool `json:"auto_notice" sql:"TINYINT"`

	IsPlaybook   bool `json:"is_playbook" sql:"TINYINT"`
	IsGameResult bool `json:"is_game_result" sql:"TINYINT"`
	IsMaxPlayer  bool `json:"is_max_player" sql:"TINYINT"`

	Dependents []string        `json:"dependents" sql:"JSON"`
	Factors    [][]game.Factor `json:"factors" sql:"JSON"`

	Configs []utils.StatConfig `json:"-"`
}
